let db = require('../db/index')
    // 生成ID
const { v4: uuidv4 } = require('uuid')

exports.get = (req, res) => { //通过userName查询数据
        let userName = req.body.userName;
        console.log(userName);
        let user_check_sql = 'select * from user where userName = "' + userName + '"';
        db.query(user_check_sql, (err, rows) => {
            if (err) {
                return res.send({ code: -1, msg: '查询失败' })
            } else {
                res.send({
                    code: 200,
                    data: rows,
                    err: err
                })
            }

            // res.send(data)
        })
    }
    //注册账号
exports.add = (req, res) => { //向user表添加数据
        let userName = req.body.userName //账号
        let passWord = req.body.passWord; // 密码
        let id = uuidv4() || Math.random().toString().slice(2); //  id
        let user_check_sql = 'select * from user where userName="' + userName + '"';
        db.query(user_check_sql, (err, rows) => {
            if (err) {
                res.send({
                    code: -1,
                    msg: '查询失败'
                })
            } else {
                if (rows && rows.length != 0) {
                    let [user] = rows;
                    if (user.userName == userName) {
                        res.send({
                            code: 201,
                            msg: '用户已存在'
                        })
                    } else {
                        res.send({
                            code: 501,
                            msg: '后端业务异常',
                            rows
                        })
                    }
                } else {
                    var sql = 'insert into user (userName,passWord,id) values ("' + userName + '","' + passWord + '","' + id + '")'
                    db.query(sql, (err) => {
                        if (err) {
                            console.log(err)
                            res.send({
                                code: -1,
                                msg: '注册失败'
                            })
                        } else {
                            res.send({
                                code: 200,
                                msg: '注册成功',
                                userName: userName
                            })
                        }
                    })
                }
            }
        })

    }
    //添加成绩
exports.addscore = (req, res) => {
    //成绩
    let active = req.body.active;
    let yuwen = req.body.yuwen;
    let shuxue = req.body.shuxue;
    let yingyu = req.body.yingyu;
    let wuli = req.body.wuli;
    let huaxue = req.body.huaxue;
    let shengwu = req.body.shengwu;
    let lishi = req.body.lishi;
    let dili = req.body.dili;
    let zhengzhi = req.body.zhengzhi;
    //用户名和id
    let userName = req.body.userName;
    let id = uuidv4() || Math.random().toString().slice(2);

    let user_check_sql = 'select * from user where userName="' + userName + '"';

    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            var sql = 'insert into score (userName,active,yuwen,shuxue,yingyu,wuli,huaxue,shengwu,dili,lishi,zhengzhi,id) values ("' + userName + '","' + active + '","' + yuwen + '","' + shuxue + '","' + yingyu + '","' + wuli + '","' + huaxue + '","' + shengwu + '","' + dili + '","' + lishi + '","' + zhengzhi + '","' + id + '")'
            db.query(sql, (err) => {
                if (err) {
                    console.log(err);
                    res.send({
                        code: -1,
                        msg: '登记失败'
                    })
                } else {
                    res.send({
                        code: 200,
                        msg: "登记成功",

                    })
                }
            })
        }

    })
}

exports.login = (req, res) => { //查询数据(登陆)
    let passWord = req.body.passWord;
    let userName = req.body.userName;
    console.log("userName:", userName)
    console.log("passWord:", passWord)
    let user_check_sql = 'select * from user where userName="' + userName + '" and passWord ="' + passWord + '"';

    db.query(user_check_sql, (err, rows) => {
        if (err) {
            console.log(err)
            res.send({
                code: -1,
                msg: '登录失败'
            })
        } else {
            if (rows && rows.length == 0) {
                res.send({
                    code: 404,
                    msg: '账号或密码错误'
                })
            } else {
                let [{ userName, id, nickname, sign, score }] = rows;
                res.send({
                    code: 200,
                    msg: '登录成功',
                    userName,
                    id,
                    nickname,
                    sign,
                    score
                })
            }
        }
    })
}

//查询学校
exports.searchschool = (req, res) => {
    let schoolName = req.body.schoolName;
    let user_check_sql = "select * from school where schoolName like '%" + schoolName + "%'";
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            console.log(err);
            res.send({
                code: -1,
                msg: "名称查询学校失败"
            })
        } else {
            let data = rows;
            res.send({
                code: 200,
                msg: '查询成功',
                data
            })
        }
    })
}

//修改个性签名
exports.changemy = (req, res) => {
    let userName = req.body.username;
    let nickname = req.body.nickname;
    let sign = req.body.sign;
    let score = req.body.score;
    let user_check_sql = "UPDATE user SET nickname ='" + nickname + "' , sign ='" + sign + "',score ='" + score + "' WHERE userName = '" + userName + "'";
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            console.log(err);
            res.send({
                code: -1,
                msg: "修改失败"
            })
        } else {
            let data = rows;
            res.send({
                code: 200,
                msg: '修改成功',
                data
            })
        }
    })
}

//查询成绩
exports.checkScore = (req, res) => {
    let userName = req.body.userName;
    let user_check_sql = 'select * from score where userName="' + userName + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            console.log(err);
            res.send({
                code: -1,
                msg: "查询失败"
            })
        } else {
            let data = rows;
            res.send({
                code: 200,
                msg: "查询成功",
                data
            })
        }
    })
}

//查询新闻(id)
exports.checknews = (req, res) => {
    let id = req.body.id;
    let user_check_sql = 'select * from news where id ="' + id + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            console.log(err);
            res.send({
                code: -1,
                msg: "获取新闻失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "获取新闻成功",
                rows
            })
        }
    })
}

//查询新闻(title)
exports.searchnews = (req, res) => {
    let title = req.body.title;
    let user_check_sql = "select * from news where title like '%" + title + "%'";
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            console.log(err);
            res.send({
                code: -1,
                msg: "名称查询新闻失败"
            })
        } else {
            let data = rows;
            res.send({
                code: 200,
                msg: '查询成功',
                data
            })
        }
    })
}